#include <caemuplanner/CAEmuPlannerE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CAEmuPlanner::EDROOMTopContext::EDROOMTopContext (CAEmuPlanner &act, bool_t & EDROOMpVarVMoreData, CEDROOMPOOLCDCommConfig & EDROOMpPoolCDCommConfig ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,Timer(EDROOMcomponent.Timer)
	,EmuPlannerSGIControl(EDROOMcomponent.EmuPlannerSGIControl)
	, VMoreData ( EDROOMpVarVMoreData )
	, CEDROOMPoolCDCommConfig ( EDROOMpPoolCDCommConfig )
 {

}

CAEmuPlanner::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,Timer(contex.Timer)
	,EmuPlannerSGIControl(contex.EmuPlannerSGIControl)
	, VMoreData ( contex.VMoreData )
	, CEDROOMPoolCDCommConfig ( contex.CEDROOMPoolCDCommConfig )
 {

}

	// EDROOMSearchContextTrans********************************************

bool CAEmuPlanner::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CAEmuPlanner::EDROOMTopContext::FSendKeepOnComm()

 
{
	bool_t okTC=false;
	bool_t okTM=false;
	/* Cuando se produce estra transicion es que se ha completado una comunicacion
	 *	TC-TM -> Comprobamos que cuadran las imagenes
	 */
 
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TC
	join_multi_tc_files();
	okTC=check_multi_tc_files();
#endif
 
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TM
	if (VMoreData==false)
	{
		join_multi_tm_files();
		okTM=check_multi_tm_files();
	}
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TC
#ifdef CONFIG_DEMO_SAF_IT_SWR_DEMO
	if (VMoreData==true)
		{
			join_one_tc_files();
			okTC=check_one_tc_files();
		}
#else
	join_one_tc_files();
	okTC=check_one_tc_files();
#endif
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TM
	join_one_tm_files();
	okTM=check_one_tm_files();
#endif
 
#ifdef CONFIG_DEMO_SAF_IT_SWR_DEMO
	if (VMoreData==false)
	{
		if (okTM)
 
		{
			printf("Recibidas telemetrías en la estación de Tierra: OK\n");
		}
		else
		{
			printf("Recibidas telemetrías en la estación de Tierra: ERROR\n");
		}
		printf("Presione ENTER para continuar con la segunda fase del demostrador\n");
		getchar();
		VMoreData=true;
		fclose(fopen("FOneTC.jpg","wb"));
		printf("EmuPlanner: sending SKeepOnComm\n");
		CDCommConfig * ap_CDCommConfig = CEDROOMPoolCDCommConfig.AllocData();
 
		// Complete Data
 
		// invoke
 
		MsgBack=EmuPlannerSGIControl.invoke(SKeepOnComm, EDROOMprioVeryHigh, ap_CDCommConfig, &CEDROOMPoolCDCommConfig);
 
		// Handling MsgBack
 
		// End Handling MsgBack
 
		EDROOMFreeMsgBack(MsgBack);
	}
	else
	{
		if (okTC)
 
		{
			printf("Recibidos telecomandos en el satélite: OK\n");
		}
		else
		{
			printf("Recibidos telecomandos en el satélite: ERROR\n");
		}
		image_to_frames_bidireccional_desc ();
		//exit(0);//TODO: TEMPORAL
 
	}
 
#else
	if (okTC && okTM)
	{
		printf("Test: OK\n");
	}
	else
	{
		printf("Test: ERROR\n");
	}
 
//TODO: probar a pasar a otra transmision mas
 
//	printf("EmuPlanner: sending KEEP\n");
//	CDCommConfig * ap_CDCommConfig = CEDROOMPoolCDCommConfig.AllocData();
//
//		// Complete Data
//
//		// invoke
//
//	MsgBack=EmuPlannerSGIControl.invoke(SKeepOnComm, EDROOMprioVeryHigh, ap_CDCommConfig, &CEDROOMPoolCDCommConfig);
//
//		// Handling MsgBack
//
//		// End Handling MsgBack
//
//	EDROOMFreeMsgBack(MsgBack);
 
	exit(0);//TODO: TEMPORAL
#endif
 
}



void	CAEmuPlanner::EDROOMTopContext::FSendToSgi()

 
{
	printf("Comienza el pase de HumSAT-UAH\n");
	CDCommConfig * ap_CDCommConfig = CEDROOMPoolCDCommConfig.AllocData();
	// Complete Data
	//ap_CDCommConfig
 
	// invoke
	MsgBack=EmuPlannerSGIControl.invoke(SToComm, EDROOMprioNormal, ap_CDCommConfig, &CEDROOMPoolCDCommConfig);
	
	// Handling MsgBack
	// End Handling MsgBack
	EDROOMFreeMsgBack(MsgBack);
}



void	CAEmuPlanner::EDROOMTopContext::FTimerEmuPlanner()

 
{
#ifdef CONFIG_DEMO_SAF_IT_SWR_DEMO
 	Pr_Time	interval(30.0); // Declare time var with an initial value in seconds
#else
 	Pr_Time	interval(2.0); // Declare time var with an initial value in seconds
#endif
 
	Timer.InformIn( interval, EDROOMprioNormal );
}
 



	//*****Pools

	//CEDROOMPOOLCDCommConfig

CAEmuPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::CEDROOMPOOLCDCommConfig(unsigned int elemCount, CDCommConfig* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDCommConfig)){;}

CDCommConfig *	CAEmuPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::AllocData(){
	return(CDCommConfig*)CEDROOMProtectedMemoryPool::AllocData();
}



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CAEmuPlanner::EDROOMTopState::EDROOMTopState (CAEmuPlanner &act, CEDROOMMemory *pEDROOMMemory  ):
	  EDROOMTopContext( act , VMoreData,CEDROOMPoolCDCommConfig )
	, VMoreData ( false )
	, CEDROOMPoolCDCommConfig ( 3, pEDROOMMemory->poolCDCommConfig, pEDROOMMemory->poolMarkCDCommConfig)
{

}

	// EDROOMBehaviour**********************************************

void CAEmuPlanner::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				edroomNextState = Ready;
				break;
			case(SendToSgi):
				FSendToSgi();
				edroomNextState = Listening;
				break;
			case(TMCompleted):
				FSendKeepOnComm();
				edroomNextState = Listening;
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Ready):
				FTimerEmuPlanner();
				edroomCurrentTrans=EDROOMReadyllegada();
				break;

			case(Listening):
				edroomCurrentTrans=EDROOMListeningllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CAEmuPlanner::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CAEmuPlanner::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Ready

	// ***********************************************************************



TEDROOMTransId CAEmuPlanner::EDROOMTopState::EDROOMReadyllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (EDROOMSignalTimeout): 

				 if ( *Msg->GetPInterface() == Timer) {

					edroomCurrentTrans.localId= SendToSgi;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Listening

	// ***********************************************************************



TEDROOMTransId CAEmuPlanner::EDROOMTopState::EDROOMListeningllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (STMTransmited): 

				 {
					edroomCurrentTrans.localId= TMCompleted;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



